<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017\10\14 0014
 * Time: 16:32
 */
namespace Home\Controller;
use Home\Controller\CommonController;

class AdminController extends CommonController{
    //管理员
    public function index(){
            $user = M("role")->alias('r')->join("tb_user u on u.role_id=r.id")->select();
            $usercom=M("user")->count();
            $this->assign("usercom",$usercom);
            $this->assign("user", $user);
            $this->display();

    }
    public function add(){
        if(IS_POST){
            $post=I("post.");
            $post['password']=sha1($post['password']);
            $add = M("user")->data($post)->add();
            if($add){
                echo true;
            }else{
                echo false;
            }
        }else{
            $role= M("role")->select();
            $this->assign("role",$role);
            $this->display();
        }
    }
    public function account_check(){
        $get=I("get.");
        $bol=M("user")->where("account='{$get['account']}'")->select();
        if($bol){
            echo 1;
        }else{
            echo 0;
        }
    }
    public function edit(){
        if(IS_POST){
            $post=I("post.");
            $post["password"]=sha1($post["password"]);
           $bol = M("user")->where("id={$_GET['id']}")->data($post)->save();
            if($bol){
                echo json_encode($bol);
            }else{
                echo false;
            }
        }else{
            $userinfo = M("user")->where("id={$_GET['id']}")->select();
            if($_GET['id']==1){
                $role=M("role")->where("id={$userinfo[0]['role_id']}")->select();
            }else{
                $role= M("role")->select();
            }
            $this->assign("user", $userinfo);
            $this->assign("role",$role);
            $this->display();
        }
    }
    public function del(){
        $del=M("user")->where("id={$_REQUEST['id']}")->delete();
        if($del){
            echo 1;
        }else{
            echo 0;
        }
    }
    public function pw_edit(){
        if(IS_POST){
            $pw['password'] = sha1(I("post.password"));
            $sel =M("user")->where("id={$_SESSION['uid']} and password={$pw['password'] }")->select();
            if($sel){
                $this->error("新旧密码相同!","/admin/pw_edit",1);
            }else{
                $bol= M("user")->where("id={$_SESSION['uid']}")->data($pw)->save();
                if($bol){
                    $this->success("修改成功!","/admin/index",1);
                }else{
                    $this->error("修改失败!","/admin/pw_edit",3);
                }
            }

        }else{
            $this->display();
        }
    }
    /**
     * 清理缓存，待完善
     */
    public function cache_clean() {
        function delDirAndFile($path, $delDir = FALSE) {
            if (is_array($path)) {
                foreach ($path as $subPath)
                    delDirAndFile($subPath, $delDir);
            }
            if (is_dir($path)) {
                $handle = opendir($path);
                if ($handle) {
                    while (false !== ( $item = readdir($handle) )) {
                        if ($item != "." && $item != "..")
                            is_dir("$path/$item") ? delDirAndFile("$path/$item", $delDir) : unlink("$path/$item");
                    }
                    closedir($handle);
                    if ($delDir)
                        return rmdir($path);
                }
            } else {
                if (file_exists($path)) {
                    return unlink($path);
                } else {
                    return FALSE;
                }
            }
            clearstatcache();
        }
        echo "<span style='color: red;'>缓存清理中……</span><br/>";
        $path =RUNTIME_PATH ."Cache/";
        echo delDirAndFile($path);
        echo "<br/><span style='color: red;'>缓存清理完毕。</span>";
    }
    //空方法
    public function _empty(){
        $this->display("没有这个方法");
    }
}